#!/usr/bin/env python
import sys
import os
try:
    import http.server as SimpleHTTPServer # Python 3
except ImportError:
    import SimpleHTTPServer
try:
    import socketserver  # Python 3
except ImportError:
    import SocketServer as socketserver

if len(sys.argv) < 2:
  exit(2)

try:
  os.chdir(sys.argv[1])
  print("Changing directory to:", sys.argv[1])
except:
  print("Error changing directory to:", sys.argv[1])

if len(sys.argv) > 2:
    PORT = int(sys.argv[2])
else:
    PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = socketserver.TCPServer(("", PORT), Handler)

print("serving at port", PORT)
httpd.serve_forever()
